package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGrouponMapper;
import com.qf.pojo.DtsGroupon;
import com.qf.service.DtsGoodsService;
import com.qf.service.DtsGrouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class DtsGrouponServiceImpl implements DtsGrouponService {
    @Autowired
    DtsGrouponMapper grouponMapper;
    @Autowired
    DtsGoodsService goodsService;

    @Override
    public Map<String, Object> queryIpage(Integer page, Integer limit, Integer goodsId, String sort, String order) {
        IPage<DtsGroupon> ipage = new Page<>(page, limit);
        QueryWrapper<DtsGroupon> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(goodsId)) {
            wrapper.eq("id",goodsId);
        }
        if ("desc".equals(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }
        goodsService.selectById(goodsId);
        grouponMapper.selectPage(ipage,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("items", ipage.getRecords());
        data.put("total", ipage.getTotal());

        return data;
    }
}